package com.hengtongxing.hejiayun_employee.utils;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Time: 2019/7/15
 * Author: HWC
 * Description:
 */

public class SPManager {

    private static SPManager mInstance;

    public static SPManager getInstance() {
        if (mInstance == null) {
            mInstance = new SPManager();
        }
        return mInstance;
    }

    /**
     * 系统配置
     */
    public void saveSystemConfig(String systemConfig) {
        SPUtils.put(Key.SYSTEMCONFIG, systemConfig);
    }

    public String getSystemConfig() {
        return SPUtils.get(Key.SYSTEMCONFIG, "");
    }

    /**
     * 同意Protocol
     */
    public void saveProtocol(String protocol) {
        SPUtils.put(Key.PROTOCOL, protocol);
    }

    public String getProtocol() {
        return SPUtils.get(Key.PROTOCOL, "");
    }

    /**
     * 保存token
     */
    public void saveToken(String token) {
        SPUtils.put(Key.TOKEN, token);
    }

    public String getToken() {
        return SPUtils.get(Key.TOKEN, "");
    }

    /**
     * 语音token
     */
    public void saveSpeechToken(String token, String expireTime) {
        SPUtils.put(Key.SPEECHTOKEN, token + "_" + expireTime);
    }

    public String getSpeechToken() {
        return SPUtils.get(Key.SPEECHTOKEN, "");
    }

    /**
     * 语音播报开关
     */
    public void saveSpeechSwitch(String speechswitch) {
        SPUtils.put(Key.SPEECHSWITCH, speechswitch);
    }

    public String getSpeechSwitch() {
        return SPUtils.get(Key.SPEECHSWITCH, "true");
    }

    /**
     * 保存用户信息
     */
    public void saveUserInfoJson(String userInfoJson) {
        SPUtils.put(Key.USER_INFO_JSON, userInfoJson);
    }

    public String getUserInfoJson() {
        return SPUtils.get(Key.USER_INFO_JSON, "");
    }

    /**
     * 保存门店基本信息
     */
    public void saveStoreBasic(String store_basic) {
        SPUtils.put(Key.STORE_BASIC, store_basic);
    }

    public String getStoreBasic() {
        return SPUtils.get(Key.STORE_BASIC, "");
    }

    /**
     * 保存门店公司信息
     */
    public void saveStoreCompany(String store_company) {
        SPUtils.put(Key.STORE_COMPANY, store_company);
    }

    public String getStoreCompany() {
        return SPUtils.get(Key.STORE_COMPANY, "");
    }

    /**
     * 保存门店信息
     */
    public void saveStoreInfo(String store_info) {
        SPUtils.put(Key.STORE_INFO, store_info);
    }

    public String getStoreInfo() {
        return SPUtils.get(Key.STORE_INFO, "");
    }

    /**
     * 保存下级咨询师id
     */
    public void saveChildAgentId(String child_agent_id) {
        SPUtils.put(Key.CHILD_AGENT_ID, child_agent_id);
    }

    public String getChildAgentId() {
        return SPUtils.get(Key.CHILD_AGENT_ID, "");
    }


    /**
     * 保存用户手机
     */
    public void saveUserMobile(String mobile) {
        SPUtils.put(Key.USERMOBILE, mobile);
    }

    public String getUserMobile() {
        return SPUtils.get(Key.USERMOBILE, "");
    }

    /**
     * 保存用户密码
     */
    public void saveUserPwd(String userpwd) {
        SPUtils.put(Key.USERPWD, userpwd);
    }

    public String getUserPwd() {
        return SPUtils.get(Key.USERPWD, "");
    }

    public String getSearchRecord() {
        return SPUtils.get(Key.SEARCH_RECORD, "");
    }

    public void saveSearchRecord(String searchKey) {

        String searchRecord = getSearchRecord();
        if (TextUtils.isEmpty(searchRecord)) {
            SPUtils.put(Key.SEARCH_RECORD, searchKey);
        } else {
            String[] split = searchRecord.split(",");
            List<String> keyWordList = new ArrayList<>();
            Collections.addAll(keyWordList, split);
            String key = searchKey.split("_")[0];
            String selectData = "";
            for (int i = 0; i < keyWordList.size(); i++) {
                if (keyWordList.get(i).contains(key)) {
                    selectData = keyWordList.get(i);
                }
            }
            if (!TextUtils.isEmpty(selectData)) {
                keyWordList.remove(selectData);
            }
            if (keyWordList.size() >= 10) {
                keyWordList.add(0, searchKey);
                keyWordList.remove(keyWordList.size() - 1);
            } else {
                keyWordList.add(0, searchKey);
            }

            StringBuilder stringBuilder = new StringBuilder();

            for (String s : keyWordList) {
                stringBuilder.append(s);
                stringBuilder.append(",");
            }

            SPUtils.put(Key.SEARCH_RECORD, stringBuilder.toString());
        }
    }

    public void deleteSearchRecord(String searchKey) {
        String searchRecord = getSearchRecord();
        if (!TextUtils.isEmpty(searchRecord)) {
            String[] split = searchRecord.split(",");
            List<String> keyWordList = new ArrayList<>();
            Collections.addAll(keyWordList, split);
            keyWordList.remove(searchKey);
            StringBuilder stringBuilder = new StringBuilder();

            for (String s : keyWordList) {
                stringBuilder.append(s);
                stringBuilder.append(",");
            }
            SPUtils.put(Key.SEARCH_RECORD, stringBuilder.toString());
        }
    }

    /**
     * 退出登录清除 token
     */

    public void logOffRemove() {
        remove(Key.TOKEN);
        remove(Key.USER_INFO_JSON);
        remove(Key.SYSTEMCONFIG);
    }

    public void remove(String key) {
        SPUtils.remove(key);
    }


    public interface Key {
        String USER_INFO_JSON = "user_info_json";//用户信息的json
        String TOKEN = "token";//保存的本地token
        String USERMOBILE = "usermobile";//保存的本地usermobile
        String USERPWD = "userpwd";//保存的本地userpwd

        String SYSTEMCONFIG = "SystemConfig";//保存的本地userpwd

        String STORE_BASIC = "store_basic";//门店基本信息
        String STORE_COMPANY = "store_company";//门店公司信息
        String STORE_INFO = "store_info";//门店信息
        String SEARCH_RECORD = "search_record";

        String PROTOCOL = "protocol";
        String CHILD_AGENT_ID = "child_agent_id";

        String SPEECHTOKEN = "speech_token";//语音token
        String SPEECHSWITCH = "speech_switch";//语音是否播报
    }

}
